package com.baidu.util;

import java.io.UnsupportedEncodingException;
import java.util.Random;


/**
 * 字符串工具类
 * @author libs
 * 2015-12-30 13:01
 */
public class StrUtil {
	
	//private final static int delta = 0x9fa5 - 0x4e00 + 1;
	/**
     * 获取指定长度随机简体中文
     * @param len int
     * @return String
     */
    public static String getRandomJianHan(int len){
        String ret="";
          for(int i=0;i<len;i++){
              String str = null;
              int hightPos, lowPos; // 定义高低位
              Random random = new Random();
              hightPos = (176 + Math.abs(random.nextInt(39))); //获取高位值
              lowPos = (161 + Math.abs(random.nextInt(93))); //获取低位值
              byte[] b = new byte[2];
              b[0] = (new Integer(hightPos).byteValue());
              b[1] = (new Integer(lowPos).byteValue());
              try{
                  str = new String(b, "GBk"); //转成中文
              }catch (UnsupportedEncodingException ex){
                  ex.printStackTrace();
              }
               ret+=str;
          }
      return ret;
    }
    /**
     * 截取字符串
     * @param str 原字符串
     * @param s1 起始字符串
     * @param s2 结束字符串
     * @return
     */
    public static String substring(String str, String s1, String s2) {
		// 1、先获得0-s1的字符串，得到新的字符串sb1
		// 2、从sb1中开始0-s2获得最终的结果。
		try {
			StringBuffer sb = new StringBuffer(str);
			String sb1 = sb.substring(sb.indexOf(s1) + s1.length());
			return String.valueOf(sb1.substring(0, sb1.indexOf(s2)));
		} catch (StringIndexOutOfBoundsException e) {
			return str;
		}
	}
}
